/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.comfacauca.xmlc2.servlet;

import com.comfacauca.xmlc2.dao.CombosDAO;
import com.comfacauca.xmlc2.dao.ProyectosDAO;
import com.comfacauca.xmlc2.entidades.Area;
import com.comfacauca.xmlc2.entidades.Combos;
import com.comfacauca.xmlc2.entidades.Estado;
import com.comfacauca.xmlc2.entidades.Etapa;
import com.comfacauca.xmlc2.entidades.Iteracion;
import com.comfacauca.xmlc2.entidades.Motivo;
import com.comfacauca.xmlc2.entidades.MotivoExtra;
import com.comfacauca.xmlc2.entidades.Prioridad;
import com.comfacauca.xmlc2.entidades.Proyecto;
import com.comfacauca.xmlc2.entidades.Responsable;
import com.comfacauca.xmlc2.entidades.TipoActividad;
import com.comfacauca.xmlc2.entidades.Tiporequerimiento;
import com.comfacauca.xmlc2.entidades.glpi.GlpiTickets;
import com.comfacauca.xmlc2.entidades.glpi.GlpiUsers;
import com.comfacauca.xmlc2.servicio.CombosI;
import com.comfacauca.xmlc2.servicio.ProyectosI;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author eorozco
 */
@WebServlet(name = "CombosServlet", urlPatterns = {"/CombosServlet"})
public class CombosServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    CombosI combosI = new CombosDAO();
    ProyectosI proyectosI = new ProyectosDAO();
    
    List combo = new ArrayList<Object>();
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            int tipo =Integer.parseInt(request.getParameter("tipo"));
            
            switch(tipo){
                case 0:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Responsable m : (List<Responsable>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getLogin()+"</option>");
                      }
                    break;
                    
                case 1:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Proyecto m : (List<Proyecto>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                    
                case 2:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Iteracion m : (List<Iteracion>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getCodigo()+" "+m.getIdProyecto().getDescripcion()+"</option>");
                      }
                    break;
                    
                case 3:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Estado m : (List<Estado>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                    
                case 4:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Tiporequerimiento m : (List<Tiporequerimiento>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                    
                case 5:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Prioridad m : (List<Prioridad>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                    
                case 6:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Etapa m : (List<Etapa>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
            
                case 7:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Motivo m : (List<Motivo>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                    
                case 8:
                    combo = combosI.traerCombo(request.getParameter("tabla"), "glpiPU");                    
                    out.println("<option value='0'>.:Seleccione:.</option>");
                    for (GlpiUsers m : (List<GlpiUsers>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getFirstname()+" "+m.getRealname()+"</option>");
                      }
                    break;
                    
                case 9:
                    
                    HttpSession session = request.getSession();
                    Integer idresp = (Integer) session.getAttribute("id");
                    
                    Responsable responsable = new Responsable();
                    
                    responsable = proyectosI.traerResponsable(idresp);
                    
                    combo = combosI.traerCombo(request.getParameter("tabla"), "glpiPU", responsable.getUsrglpi());                    
                    out.println("<option value='0'>.:Seleccione:.</option>");
                    for (GlpiTickets m : (List<GlpiTickets>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getId()+"-"+m.getName()+"</option>");
                      }
                    break;
                    
                 case 10:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (Area m : (List<Area>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                 case 11:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (TipoActividad m : (List<TipoActividad>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;
                     
                  case 12:
                    combo = combosI.traerCombo(request.getParameter("tabla"));                    
                    out.println("<option value=''>.:Seleccione:.</option>");
                    for (MotivoExtra m : (List<MotivoExtra>) combo) {                    
                        out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
                      }
                    break;   
                     
                    
            }
            
            
//            if(==0){            
//                combo = combosI.traerCombo(request.getParameter("tabla"));    
//                
//                out.println("<option value=''>.:Seleccione:.</option>");
//                for (Combos m : (List<Combos>) combo) {                    
//                    out.println("<option value='"+m.getId()+"'>"+m.getDescripcion()+"</option>");
//                  }
//            }else{
//                combo = combosI.traerCombo(Integer.parseInt(request.getParameter("id")),"Responsable");
//            }
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
